﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using Repository.Core.Models.Dto;
using Repository.Core.Models.Queries;
using Repository.Core.Models.VO;
using Repository.Core.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository.Core.Controllers
{
    [NoCache, ValidationFilter]
    public class WorkSpaceController : MvcControllerBase
    {
        protected IWorkSpaceService CreateComponent(string repoName)
        {
            return this.Container.GetComponent<IWorkSpaceService>(repoName, this.User);
        }

        [HttpPost]
        public Response<ResultObject<IEnumerable<WorkItemVo>>> Query([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] PagedQueryObject<CommonQuery> request)
        {
            var result = this.CreateComponent(repoName).Query(request);

            return result.CreateResponse();
        }

        [HttpGet]
        public Response<WorkItemVo> Item([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromQuery] string name)
        {
            var result = this.CreateComponent(repoName).GetItem(name);

            return result.CreateResponse();
        }

        [HttpPost]
        public Response<WorkItemVo> Create([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] WorkItemDto request)
        {
            var result = this.CreateComponent(repoName).Create(request);

            return result.CreateResponse();
        }

        [HttpPost]
        public Response<WorkItemVo> Update([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] WorkItemDto request)
        {
            var result = this.CreateComponent(repoName).Update(request);

            return result.CreateResponse();
        }

        [HttpPost]
        public Response Delete([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] string name)
        {
            this.CreateComponent(repoName).Delete(name);

            return new();
        }

    }
}
